package com.zh.view.component

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.zh.view.ChatMsg


@Composable
fun ChatBubble(data: ChatMsg, isRight: Boolean) {
    val alignment = if (isRight) Alignment.End else Alignment.Start
    val msgColor = if (isRight) Color(0xFFD8BFD8) else Color(0xFFFFB6C1)

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 4.dp)
            .wrapContentWidth(alignment)
    ) {
        Column(
            modifier = Modifier.padding(horizontal = if (isRight) 8.dp else 0.dp),
            horizontalAlignment = alignment
        ) {
            Card(
                modifier = Modifier.padding(horizontal = if (isRight) 0.dp else 8.dp),
                shape = RoundedCornerShape(
                    topStart = 16.dp,
                    topEnd = 16.dp,
                    bottomStart = if (isRight) 16.dp else 0.dp,
                    bottomEnd = if (isRight) 0.dp else 16.dp
                ),
                colors = CardDefaults.cardColors(containerColor = msgColor),
                elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
            ) {
                SelectionContainer {
                    Text(
                        text = data.message,
                        modifier = Modifier.padding(12.dp),
                        color = Color.Black,
                        fontSize = 16.sp
                    )
                }
            }

            if (data.from.isNotEmpty()) {
                Text(
                    text = data.from,
                    modifier = Modifier.padding(
                        start = 12.dp,  // 右侧气泡增加左侧内边距
                        end = 12.dp,  // 左侧气泡增加右侧内边距
                    ),
                    color = Color.Gray,
                    fontSize = 12.sp,
                    maxLines = 1
                )
            }
        }
    }
}
